package org.example.xsda.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaMode;
import org.example.xsda.common.Result;
import org.example.xsda.dto.ImportResultDTO;
import org.example.xsda.service.ImportExportService;
import org.example.xsda.vo.ImportHistoryVO;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/import-export")
public class ImportExportController {
    
    @Resource
    private ImportExportService importExportService;
    
    @PostMapping("/import/student")
    @SaCheckRole("admin")
    public Result<ImportResultDTO> importStudent(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return Result.error("文件不能为空");
        }
        
        ImportResultDTO result = importExportService.importStudent(file);
        return Result.success(result);
    }
    
    @PostMapping("/import/score")
    @SaCheckRole(value = {"admin", "teacher"}, mode = SaMode.OR)
    public Result<ImportResultDTO> importScore(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return Result.error("文件不能为空");
        }
        
        ImportResultDTO result = importExportService.importScore(file);
        return Result.success(result);
    }
    
    @GetMapping("/import/history")
    @SaCheckRole(value = {"admin", "teacher"}, mode = SaMode.OR)
    public Result<List<ImportHistoryVO>> getImportHistory() {
        List<ImportHistoryVO> history = importExportService.getImportHistory();
        return Result.success(history);
    }
    
    @GetMapping("/template/download")
    @SaCheckRole(value = {"admin", "teacher"}, mode = SaMode.OR)
    public void downloadTemplate(@RequestParam String type, HttpServletResponse response) {
        importExportService.downloadTemplate(type, response);
    }
    
    @GetMapping("/export/student")
    @SaCheckRole(value = {"admin", "teacher"}, mode = SaMode.OR)
    public void exportStudent(HttpServletResponse response) {
        importExportService.exportStudent(response);
    }
    
    @GetMapping("/export/score")
    @SaCheckRole(value = {"admin", "teacher"}, mode = SaMode.OR)
    public void exportScore(@RequestParam String semester, HttpServletResponse response) {
        importExportService.exportScore(semester, response);
    }
    
    @GetMapping("/export/summary")
    @SaCheckRole(value = {"admin", "teacher"}, mode = SaMode.OR)
    public void exportSummary(HttpServletResponse response) {
        importExportService.exportSummary(response);
    }
    
    @GetMapping("/export/record")
    @SaCheckRole(value = {"admin", "teacher"}, mode = SaMode.OR)
    public void exportRecord(HttpServletResponse response) {
        importExportService.exportRecord(response);
    }
    
    @GetMapping("/export/package")
    @SaCheckRole(value = {"admin", "teacher"}, mode = SaMode.OR)
    public void exportPackage(@RequestParam String studentNo, HttpServletResponse response) {
        importExportService.exportPackage(studentNo, response);
    }
}

